package com.pragmaticminds.forrest.geometry;
import java.awt.Point;

public class Position {

    private double positionX;
    private double positionY;
    
    public Position(){
        this.positionX = 0;
        this.positionY = 0;
    }

    public Position(double x, double y){
        setPosition( x, y);
    }
    
    public Position(Point p){
        this.positionX = p.getX();
        this.positionY = p.getY();
    }
    
    public Position(Position p){
        this.positionX = p.getPositionX();
        this.positionY = p.getPositionY();
    }    

    public Position sum(Position p){
        return  new Position( this.positionX + p.getPositionX(), this.positionY + p.getPositionY() );
    }
          
    public Position subtract(Position p){
        return new Position( this.positionX - p.getPositionX(), this.positionY - p.getPositionY() );        
    }
    
    public double getDistanceTo(Position p){
        return subtract(p).getMagnitude();
    }

    public double getMagnitude(){
        return Math.sqrt(this.positionX * this.positionX + this.positionY * this.positionY );
    }
    
    public void setMagnitude(double d){
        if(this.getMagnitude() > Function.EPSILON){            
            this.positionX *= d / getMagnitude();
            this.positionY *= d / getMagnitude();
        }             
    }

    public void setPosition(double x, double y){
        this.positionX = x;
        this.positionY = y;
    }
    
       
    public static double normalizeAngle( double angle ){
      while( angle > 180.0  ) angle -= 360.0;
      while( angle < -180.0 ) angle += 360.0;
      return ( angle );
    }

    public double getDirection(){
        return Function.atan2Deg(this.positionY, this.positionX);
    }          
  
    public double getPositionX() {
        return this.positionX;
    }

    public void setPositionX(double m_x) {
        this.positionX = m_x;
    }

    public double getPositionY() {
        return this.positionY;
    }

    public void setPositionY(double m_y) {
        this.positionY = m_y;
    }
    
}
